﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProject.Models;
using MvcProject.Utils;

namespace MvcProject.Controllers
{
    public class TemporaryWorkersController : Controller
    {
        private BaseModels db = new BaseModels();
        //
        // GET: /TemporaryWorkers/

        public ActionResult Index()
        {
            var contracts = db.FindAllTemporaryWorkers();
            return View(contracts);
        }

        //
        // GET: /TemporaryWorkers/Details/5

        public ActionResult Details(int id)
        {
            TemporaryWorkers dinner = db.GetTemporaryWorker(id);
            return View(dinner);
        }

        //
        // GET: /TemporaryWorkers/Create

        public ActionResult Create()
        {
            ViewData["projectinfos"] = new SelectList(new ProjectInfoModel().FindAllProjectInfos(), "ProjectId", "ProjectName");
            ViewData["teams"] = new SelectList(new BaseModels().FindAllTeams(), "TeamId", "TeamName");
            ViewData["worktypes"] = new SelectList(new BaseModels().FindAllWorkTypes(), "WorkTypeId", "WorkTypeName");
            return View();
        } 

        //
        // POST: /TemporaryWorkers/Create

        [HttpPost]
        public ActionResult Create(TemporaryWorkers t)
        {
            SelectList projectinfos = new SelectList(new ProjectInfoModel().FindAllProjectInfos(), "ProjectId", "ProjectName");
            ViewData["projectinfos"] = projectinfos;
            SelectList teams = new SelectList(new BaseModels().FindAllTeams(), "TeamId", "TeamName");
            ViewData["teams"] = teams;
            SelectList worktypes = new SelectList(new BaseModels().FindAllWorkTypes(), "WorkTypeId", "WorkTypeName");
            ViewData["worktypes"] = worktypes;
            if (ModelState.IsValid)
                try
                {
                    UpdateModel(t);
                    t.CreateDate = System.DateTime.Now;
                    t.CreatUserName = "ywx";
                    t.ProjectName = projectinfos.Where(d => d.Value == t.ProjectId).Select(d => d.Text).First();
                    t.WorkType = worktypes.Where(d => d.Value == t.WorkTypeId).Select(d => d.Text).First();
                    t.TeamName = teams.Where(d => d.Value == t.TeamId).Select(d => d.Text).First(); 
                    db.Add(t);
                    db.Save();
                    return RedirectToAction("Edit", new { id = t.guid});
                }
                catch (Exception e)
                {
                    return View(t);
                }
            return View(t);
        }
        
        //
        // GET: /TemporaryWorkers/Edit/5
 
        public ActionResult Edit(int id)
        {
            IEnumerable<MvcProject.Models.AttachInfo> attachinfos_EmployeeCardAttachment = new BaseModels().GetAttachInfos(AttachinfoEnum.TemporaryWorkers.ToString(), AttachinfoEnum.EmployeeCard.ToString(), id);
            ViewData["attachinfos_EmployeeCardAttachment"] = attachinfos_EmployeeCardAttachment;
            IEnumerable<MvcProject.Models.AttachInfo> attachinfos_IdCardAttachment = new BaseModels().GetAttachInfos(AttachinfoEnum.TemporaryWorkers.ToString(), AttachinfoEnum.IdCard.ToString(), id);
            ViewData["attachinfos_IdCardAttachment"] = attachinfos_IdCardAttachment;
            IEnumerable<MvcProject.Models.AttachInfo> attachinfos_ContractAttachment = new BaseModels().GetAttachInfos(AttachinfoEnum.TemporaryWorkers.ToString(), AttachinfoEnum.Contract.ToString(), id);
            ViewData["attachinfos_ContractAttachment"] = attachinfos_ContractAttachment;
            TemporaryWorkers dinner = db.GetTemporaryWorker(id);
            return View(dinner);
        }

        //
        // POST: /TemporaryWorkers/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TemporaryWorkers/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /TemporaryWorkers/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
